﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class DefaultController : Controller
    {
        private SchoolEntities _context;
        public DefaultController()
        {
            _context = new SchoolEntities();
        }
        // GET: Default
        public ActionResult Index()
        {
            var courses = _context.Courses.ToList();
            return View(courses);
        }

        public ActionResult Modify(int id)
        {
            
            var course = _context.Courses.Find(id);
            return View(course);
        }
       
        [HttpPost]
        public ActionResult Modify(Course course)
        {
            //var id = Convert.ToInt32(Request["CourseID"]);
            //var title = Request["Title"];
            //var credits = Convert.ToInt32(Request["Credits"]);

            //var course = _context.Courses.Find(id);
            //course.Title = title;
            //course.Credits = credits;
            //_context.SaveChanges();
            //把实体与EF数据库建立起联系
            _context.Entry(course).State = System.Data.Entity.EntityState.Modified;
            _context.SaveChanges();
            return RedirectToAction("Index");
        }

        public  ActionResult Remove(int id)
        {
           
                var course = _context.Courses.Find(id);
                _context.Courses.Remove(course);
                _context.SaveChanges();
                return RedirectToAction("index");
           
            
        }

       
    }
}